<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;
use app\api\model\User as UserModel;
use think\db\Expression;
use app\common\library\Upload;

/**
 * 登录接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //验证
    public function verify($param)
    {
        if (empty($param['data']['car_vin'])) {
            return '请输入车架号';
        }

    }
    //查询订单

    /**
     * 订单列表
     *
     */
    public function order_select()
    {
        $param                = request()->param();
        $User                 = $this->getUser(2);
        $param['pageSize']    = isset($param['pageSize']) ? $param['pageSize'] : 10;
        $param['pageCurrent'] = isset($param['pageCurrent']) ? ($param['pageCurrent'] - 1) * $param['pageSize'] : 0;
        $where                = '1=1';
        if (isset($param['status']) && $param['status'] !== '') {
            $where .= " and status = '{$param['status']}'";
        }

        if (!empty($param['name'])) {
            $where .= " and (car_num like '%{$param['name']}%' or car_vin like '%{$param['name']}%' or car_engine like '%{$param['name']}%' or car_owner like '%{$param['name']}%')";
        }
        //审核人
        if (!empty($param['shenhe_peo'])) {
            $where .= " and shenhe_peo like '%{$param['shenhe_peo']}%'";
        }
        //录入人
        if (!empty($param['yw_belonger'])) {
            $where .= " and yw_belonger like '%{$param['yw_belonger']}%'";
        }

        if (!empty($param['times'])) {
//            $timeRange = "2024-07-25 00:00:00 - 2024-07-31 23:59:59";
            $timeRange = $param['times'];
            // 分割时间段字符串
            $timeParts = explode(' - ', $timeRange);
            if (count($timeParts) != 2) {
                echo "时间段字符串格式不正确";
            }// 将起始时间和结束时间转换为时间戳
            $startTimeStr = strtotime($timeParts[0]);
            $endTimeStr   = strtotime($timeParts[1]);

            $where .= " and plan_strattime >= {$startTimeStr} and plan_strattime <= {$endTimeStr}";
        }
        //查询对应组织
        if (!empty($param['zuzhi'])) {
            $zuzhi = zuzhi($param['zuzhi_id']);
            $where .= " and yw_class in ({$zuzhi})";
        } else {
            $where .= " and yw_class in ({$User['admin']['zuzhi_id']})";
        }
//        dump($where);die;
        // 查询列表
        $List = Db::name('list_one')
            ->where($where)
            ->where('deletetime',null)
            ->order('id DESC')
            ->limit($param['pageCurrent'], $param['pageSize'])
            ->select();
//        dump($List);die;
        foreach ($List as $k => &$item) {
            $item['zuzhi']          = Db::name('zuzhi')->where(['id' => $item['yw_class']])->value('name');
            $item['plan_strattime'] = date('Y-m-d H:i:s', $item['plan_strattime']);
            $item['plan_endtime']   = date('Y-m-d H:i:s', $item['plan_endtime']);
            $item['yw_hetong_time'] = $item['yw_hetong_time'] ? date('Y-m-d H:i:s', $item['yw_hetong_time']) : "-";

            //查询签字
            $sign         = Db::name('sign')->where(['orderid' => $item['id']])->find();
            $item['sign'] = $sign;
        }
        $all_main_free                  = Db::name('list_one')
            ->where($where)
            ->field('sum(main_free) as all_main_free,sum(main_sum) as all_main_sum')
            ->find();
        $all_main_free['all_main_free'] = bcadd($all_main_free['all_main_free'], 0, 2);
        $all_main_free['all_main_sum']  = bcadd($all_main_free['all_main_sum'], 0, 2);
        $data['list']                   = $List;
        $data['num']                    = $all_main_free;
        $this->success('请求成功', $data);
    }

    /**
     * 删除
     * @return mixed
     */
    public function order_del()
    {
        $param = request()->param();
        Db::name('list_one')->where(['id' => $param['id']])->update(['deletetime' => time()]);
        $this->success('请求成功');
    }

    /**
     * 重新签名
     * @return mixed
     */
    public function chong_qian()
    {
        $param = request()->param();
        Db::name('sign')->where(['orderid' => $param['id']])->delete();
        $this->success('请求成功');
    }

    //更新暂存单号
    public function up_danahao()
    {
        //查询
        $data = request()->param();

        $id  = $data['id'];
        $res = Db::name('list_one')->where('id', $id)
            ->field('id,ids,yw_come,yw_class,yw_peo,yw_peo_phone,yw_belonger,yw_handle,yw_join_time,yw_enter_peo,yw_hetongpeo,yw_hetong_time,yw_finance_peo,yw_shenhe_time,tong_storage,tong_number,tong_document,give_company,give_starttime,give_endtime,car_num,car_is_new,car_num_kind,haocolor,car_province,car_check,car_check_res,car_owner,car_card_kind,car_card,car_relation,car_address,car_vin,car_engine,car_factory_num,car_register,car_age,car_issue_time,car_is_transfer,car_buy,car_reall,car_use_nature,car_leixing,car_kind,car_carry_peo,car_carry_quality,car_curb_weight,car_displacement,car_power,car_lastyear,car_rank,plan_strattime,plan_endtime,main_name,main_money,main_standard,main_discount,main_submit,main_franchise,main_disan,main_santong,main_biao,main_zhe,main_yingjiao,main_ji,dao_name,dao_tong,dao_biao,dao_zhe,dao_ying,dao_ji,siji,siji_tong,siji_biao,siji_zhe,siji_ying,siji_ji,chengke,ck_money,chengke_zuo,ck_tong,ck_biao,ck_zhe,ck_ying,ck_ji,buji,bu_tong,bu_biao,bu_zhe,bu_ying,bu_buji,ziran,zi_tong,zi_biao,zi_zhe,zi_ying,zi_buji,fdjsunshi,fdj_tong,fdj_biao,fdj_zhe,fdj_ying,fdj_buji,zdxiuli,zd_tong,zd_biao,zd_zhe,zd_ying,zd_buji,huowu,hw_tong,hw_biao,hw_zhe,hw_ying,hw_buji,boli,bl_tong,bl_biao,bl_zhe,bl_ying,bl_buji,main_sum,main_sumdiscount,main_free,main_netrate,main_netcost,ginseng_name,ginseng_kind,ginseng_card,ginseng_phone,ginseng_email,ginseng_address,learned_name,learned_kind,learned_num,learned_phone,learned_email,learned_address,resolvent,special,invoice_kind,invoice_name,invoice_num,invoice_bank,bank_address,invoice_phone,beizhu,zhanghao,file,file_name,shuoming,shenhe_time,shenhe_peo,shenhe_yijian,status,statuss,pigai_type,pigai_numz,pigai_nump,pigai_piwen,pigai_ctime,pigai_pddzh,main_tongchoufee_heji,main_xiaoguan_bili,main_xiaoguan,main_yunying_bili,main_yunying,main_tongchoujingfee_heji,dong_fee,ncd,give_number,kehu_sumdiscount,chexing,chexing_ci,feiyong,jiafei,jiaofei,jiaofei_price,jiaofei_time,jiaofei_type,trade_no,out_trade_no,type,lururen,lururen_id,feiyong_type,feiyong_qita,jiafei_type,shui_lv1,shui_lv,ziran_c,fd_c,zd_c,huowu_c,boli_c,urls1,urls2,tcxm,status_type,qianzi_status,pdf,chezhu,tk_type,bjt,gs_type,zq_id')
            ->find();


        if ($res['jiaofei'] == 1) {
            //  dump(123);die;
            //判断暂存单号是否是今天的
            $time  = substr($res['tong_storage'], 0, 4) . '-' . substr($res['tong_storage'], 4, 2) . '-' . substr($res['tong_storage'], 6, 2);
            $table = 'list_one';
            $field = 'tong_storage';
            $prev  = '';
            //1、获取本商户的最新进货单号
            $newBillNo = db($table)->field($field)->order($field . ' desc')->select();
            //2、拆分获取到的最新单号字符串
            //3、判断时间是否为今天。若是：时间不变，流水号自增；若不是：时间为今日，流水号置00001。
            //获取当前时间的年月日
            $time = date('Ymd', time());
            if ($newBillNo == null) { //无新纪录
                $zancun = $prev . $time . '00001';
            } else { //有记录
                $billno = $newBillNo[0][$field];
                //截取最新单据的时间
                $billTime = substr($billno, strlen($prev), 8);
                if ($billTime != $time) { //是否为今天的第一单
                    $zancun = $prev . $time . '00001';
                } else {
                    $billWaterNo = substr($billno, strlen($prev) + 8, 5);
                    settype($billWaterNo, 'integer'); //将流水号转换成整型
                    $billWaterNo++;                   //流水号自增
                    //判断流水单号是否为5位
                    if ($billWaterNo < 9999) {
                        $billWaterNo = str_pad($billWaterNo, 5, '0', STR_PAD_LEFT); //左侧填充0至5位
                    }

                    $zancun = $prev . $time . $billWaterNo . rand(1, 99);
                }
            }
            //修改
            Db::name('list_one')->where('id', $id)->update(['tong_storage' => $zancun, 'openid' => '']);
            $res['tong_storage'] = $zancun;

            // dump($time);die;
        } else {
            dump(122222);
            die;
        }
        $this->success('请求成功');

    }


    /**
     * 服务单添加
     *
     */
    public function order_zancun()
    {
        $param = request()->param();
//        dump($param);die;
        $verify = $this->verify($param);
        if ($verify) {
            return json(['code' => -1, 'msg' => $verify]);
        }
        $User = $this->getUser(2);

        $data = $param['data'];

        $data['yw_join_time']   = time();
        $data['car_register']   = strtotime($data['car_register']);    //注册日期
        $data['plan_strattime'] = strtotime($data['plan_strattime']);  //服务起期
        $data['plan_endtime']   = strtotime($data['plan_endtime']);    //服务止期
        // 启动事务
        Db::startTrans();
        try {


            if (empty($param['id'])) {
                if($data['status'] == 1) {
                    //判断车架号是否符合服务
                    $isset = Db::name('list_one')
                        ->where('car_vin', $data['car_vin'])
                        ->where('status', 'in', [3])
                        ->field('id,status,plan_endtime')
                        ->where('deletetime', null)
                        ->find();
                    if ($isset) {
                        return json(['code' => -1, 'msg' => '该车正在服务中，不得重复添加']);
                    }
                    $isset2 = Db::name('list_one')
                        ->where('car_vin', $data['car_vin'])
                        ->where('status', 5)
                        ->where('deletetime', null)
                        ->field('id,status,plan_endtime')
                        ->select();
                    foreach ($isset2 as $key => $value) {
                        if ($value['status'] == 5) {
                            if ($value['plan_endtime'] > time()) {
                                return json(['code' => -1, 'msg' => '该车正在服务中，不得重复添加']);
                            }
                        }
                    }
                }

                //判断是否客户姓名重复
                if(!empty($data['learned_phone'])){
                    $learned_phone = Db::name('list_one')
                        ->where('learned_phone', $data['learned_phone'])
                        ->where('status', 'in', [0, 1, 2, 3, 4])
                        ->field('id,status,plan_endtime')
                        ->where('deletetime', null)
                        ->count();
                    if ($learned_phone >= 2) {
                        return json(['code' => -1, 'msg' => '车主手机号超过两个，禁止录入']);
                    }
                }



                $data['user_id']      = $User['admin']['id'];
                $data['yw_class']     = $User['admin']['zuzhi'];
                $data['tong_storage'] = zancun();                             //暂存单号
                $data['yw_come']      = $User['admin']['zuzhi_find']['jigou'];//业务来源
                $data['yw_belonger']  = $User['admin']['username'];           //归属人
                $data['yw_handle']    = $User['admin']['username'];           //经办人
                $data['yw_enter_peo'] = $User['admin']['username'];           //录入人
                $data['createtime']   = time();
                $res                  = Db::name('list_one')->insertGetId($data);
                $id                   = $res;
            } else {
                $id = $param['id'];
                unset($data['img']);
                $data['updatetime'] = time();
                $res                = Db::name('list_one')->where('id', $id)->update($data);
            }
            Db::name('tong_files')->where(['tong_id' => $id])->delete();
            //判断是否有图片
            if (!empty($param['img'])) {
                $urls  = explode(',', $param['img']);
                $count = count($urls);
                for ($i = 0; $i < $count; $i++) {
                    $img               = [];
                    $img['url']        = $urls[$i];
                    $img['tong_id']    = $id;
                    $img['title']      = '';
                    $img['updatetime'] = time();
                    Db::name('tong_files')->insert($img);
                }
            }
            // 提交事务
            Db::commit();
            return json(['code' => 1, 'msg' => '请求成功', 'data' => $id]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage() . '，异常行：' . $e->getLine() . '，异常路径：' . $e->getfile()]);
        }

    }

    public function s_time()
    {
        $param = request()->param();
        $time  = strtotime($param['time']);
        $data  = date("Y-m-d H:i:s", $time + (86400 * 365) - 1);
        return json(['code' => 1, 'msg' => '请求成功', 'data' => $data]);

    }

    /**
     * 订单详情
     */
    public function order_detail()
    {
        $param = $this->request->param();

        $User = $this->getUser(1);
        //查询商户信息
        $id   = $param['id'] ?? 0;
        $data = db('list_one')
            ->where('id', $id)
            ->find();
        if (!$data) {
            $this->error('系统错误');
        }
        $site = Config::get('site');
        $data['car_use_nature'] ? $data['car_use_nature'] : "1";
        $data['car_use_nature_nr'] = array_flip($site['xingzhi'])[$data['car_use_nature']];
//        dump($data['car_use_nature_nr']);die;
        //查询图片
        $data['img']            = Db::name('tong_files')->where(['tong_id' => $data['id']])->select();
        $data['plan_strattime'] = date('Y-m-d H:i:s', $data['plan_strattime']);
        $data['plan_endtime']   = date('Y-m-d H:i:s', $data['plan_endtime']);
        $data['car_register']   = date('Y-m-d', $data['car_register']);

        //审核意见
        $shen = Db::name('yijian')->where('did', $id)->order('id asc')->select();
        foreach ($shen as $k => &$v) {
            $v['shen_time'] = date('Y-m-d H:i:s', $v['shen_time']);
        }
        $data['yijian'] = $shen;
        $site = Config::get('site');
        $data['site']['bjd_beijing'] = $site['bjd_beijing'];
        $data['site']['bjd1_logo'] = $site['bjd1_logo'];
        $data['site']['bjd_phone'] = $site['bjd_phone'];
        $data['site']['bjd_jbxx'] = $site['bjd_jbxx'];
        $data['site']['bjd_xm'] = $site['bjd_xm'];
        $data['site']['bjd_bgs'] = $site['bjd_bgs'];
        $data['yw_join_time'] = date('Y-m-d',$data['yw_join_time']);
        $this->success('成功', $data);
    }

    /**
     * 订单详情
     */
    public function order_dian()
    {
        $param = $this->request->param();

//        $User = $this->getUser(1);
        //查询用户信息
        $id   = $param['id'] ?? 0;
        $data = db('list_one')
            ->where('id', $id)
            ->find();
        if (!$data) {
            $this->error('系统错误');
        }
        $site = Config::get('site');
        $data['car_use_nature'] ? $data['car_use_nature'] : "1";
        $data['car_use_nature_nr'] = array_flip($site['xingzhi'])[$data['car_use_nature']];

        //条款信息
        $tiaokuan         = Db::name('tiaokuan')->order('id desc')->limit(1)->find();
        $data['tiaokuan'] = $tiaokuan;
        $this->success('成功', $data);
    }
    /**
     * 初级审核
     * */
    //初级提交审核方法
    public function chuji()
    {
        $param             = request()->param();
        $User              = $this->getUser(1);
        $post              = $param ['data'];
        $id                = $post['id'];
        $data['did']       = $post['id'];
        $data['shen_time'] = time();
        $data['shen_peo']  = $User['admin']['username'];
        $data['user_id']   = $User['admin']['id'];
        // 启动事务
        Db::startTrans();
        try {
            if ($post['shenhe_yijian'] == '') {
                $data['shen_yijian'] = "初审通过";
            } else {
                $data['shen_yijian'] = $post['shenhe_yijian'];
            }
            $res = Db::name('yijian')->insert($data);

            $list_one = Db::name('list_one')->where('id', $id)->find();
            //  $dataa['status'] = 2;
            $data_list_one = [
                'main_sumdiscount' => $list_one['main_sumdiscount'],
                'main_money'       => $list_one['main_money'],
                'main_standard'    => $list_one['main_standard'],
                'main_discount'    => $list_one['main_discount'],
                'main_submit'      => $list_one['main_submit'],
                'main_santong'     => $list_one['main_santong'],
                'main_biao'        => $list_one['main_biao'],
                'main_zhe'         => $list_one['main_zhe'],
                'main_yingjiao'    => $list_one['main_yingjiao'],
                'siji_tong'        => $list_one['siji_tong'],
                'siji_biao'        => $list_one['siji_biao'],
                'siji_zhe'         => $list_one['siji_zhe'],
                'siji_ying'        => $list_one['siji_ying'],
                'ck_money'         => $list_one['ck_money'],
                'chengke_zuo'      => $list_one['chengke_zuo'],
                'ck_tong'          => $list_one['ck_tong'],
                'ck_biao'          => $list_one['ck_biao'],
                'ck_zhe'           => $list_one['ck_zhe'],
                'ck_ying'          => $list_one['ck_ying'],
                'dao_tong'         => $list_one['dao_tong'],
                'dao_biao'         => $list_one['dao_biao'],
                'dao_zhe'          => $list_one['dao_zhe'],
                'dao_ying'         => $list_one['dao_ying'],
                'ziran_c'          => $list_one['ziran_c'],
                'zi_tong'          => $list_one['zi_tong'],
                'zi_biao'          => $list_one['zi_biao'],
                'zi_zhe'           => $list_one['zi_zhe'],
                'zi_ying'          => $list_one['zi_ying'],
                'fd_c'             => $list_one['fd_c'],
                'fdj_tong'         => $list_one['fdj_tong'],
                'fdj_biao'         => $list_one['fdj_biao'],
                'fdj_zhe'          => $list_one['fdj_zhe'],
                'fdj_ying'         => $list_one['fdj_ying'],
                'zd_tong'          => $list_one['zd_tong'],
                'zd_biao'          => $list_one['zd_biao'],
                'zd_zhe'           => $list_one['zd_zhe'],
                'zd_ying'          => $list_one['zd_ying'],
                'huowu_c'          => $list_one['huowu_c'],
                'hw_tong'          => $list_one['hw_tong'],
                'hw_biao'          => $list_one['hw_biao'],
                'hw_zhe'           => $list_one['hw_zhe'],
                'hw_ying'          => $list_one['hw_ying'],
                'boli_c'           => $list_one['boli_c'],
                'bl_tong'          => $list_one['bl_tong'],
                'bl_biao'          => $list_one['bl_biao'],
                'bl_zhe'           => $list_one['bl_zhe'],
                'bl_ying'          => $list_one['bl_ying'],
                'bu_tong'          => $list_one['bu_tong'],
                'bu_biao'          => $list_one['bu_biao'],
                'bu_zhe'           => $list_one['bu_zhe'],
                'bu_ying'          => $list_one['bu_ying'],
                'main_sum'         => $list_one['main_sum'],
                'main_free'        => $list_one['main_free'],
            ];
            $list_one_hf   = Db::name('list_one_hf')->where('one_id', $id)->find();
            if ($list_one_hf) {
                Db::name('list_one_hf')->where('one_id', $id)->update($data_list_one);
            } else {
                $data_list_one['one_id'] = $id;
                Db::name('list_one_hf')->insert($data_list_one);
            }


            $dataa = [
                'main_sumdiscount' => $post['main_sumdiscount'],
                'main_money'       => $post['main_money'],
                'main_standard'    => $post['main_standard'],
                'main_discount'    => $post['main_discount'],
                'main_submit'      => $post['main_submit'],
                'main_santong'     => $post['main_santong'],
                'main_biao'        => $post['main_biao'],
                'main_zhe'         => $post['main_zhe'],
                'main_yingjiao'    => $post['main_yingjiao'],
                'siji_tong'        => $post['siji_tong'],
                'siji_biao'        => $post['siji_biao'],
                'siji_zhe'         => $post['siji_zhe'],
                'siji_ying'        => $post['siji_ying'],
                'ck_money'         => $post['ck_money'],
                'chengke_zuo'      => $post['chengke_zuo'],
                'ck_tong'          => $post['ck_tong'],
                'ck_biao'          => $post['ck_biao'],
                'ck_zhe'           => $post['ck_zhe'],
                'ck_ying'          => $post['ck_ying'],
                'dao_tong'         => $post['dao_tong'],
                'dao_biao'         => $post['dao_biao'],
                'dao_zhe'          => $post['dao_zhe'],
                'dao_ying'         => $post['dao_ying'],
                'ziran_c'          => $post['ziran_c'],
                'zi_tong'          => $post['zi_tong'],
                'zi_biao'          => $post['zi_biao'],
                'zi_zhe'           => $post['zi_zhe'],
                'zi_ying'          => $post['zi_ying'],
                'fd_c'             => $post['fd_c'],
                'fdj_tong'         => $post['fdj_tong'],
                'fdj_biao'         => $post['fdj_biao'],
                'fdj_zhe'          => $post['fdj_zhe'],
                'fdj_ying'         => $post['fdj_ying'],
                'zd_tong'          => $post['zd_tong'],
                'zd_biao'          => $post['zd_biao'],
                'zd_zhe'           => $post['zd_zhe'],
                'zd_ying'          => $post['zd_ying'],
                'huowu_c'          => $post['huowu_c'],
                'hw_tong'          => $post['hw_tong'],
                'hw_biao'          => $post['hw_biao'],
                'hw_zhe'           => $post['hw_zhe'],
                'hw_ying'          => $post['hw_ying'],
                'boli_c'           => $post['boli_c'],
                'bl_tong'          => $post['bl_tong'],
                'bl_biao'          => $post['bl_biao'],
                'bl_zhe'           => $post['bl_zhe'],
                'bl_ying'          => $post['bl_ying'],
                'bu_tong'          => $post['bu_tong'],
                'bu_biao'          => $post['bu_biao'],
                'bu_zhe'           => $post['bu_zhe'],
                'bu_ying'          => $post['bu_ying'],
                'main_sum'         => $post['main_sum'],
                'main_free'        => $post['main_free'],
                'yw_peo'           => $post['yw_peo'],
                'yw_peo_phone'     => $post['yw_peo_phone'],
                'kehu_sumdiscount' => $post['main_sumdiscount'],
                'special'          => $post['special'],
            ];

            $dataa['status']         = 3;
            $dataa['yw_hetong_time'] = time();
            $dataa['yw_hetongpeo']   = $post['shenhe_peo'];
            $dataa['shenhe_peo']     = $User['admin']['username'];

            $ress = Db::name('list_one')->where('id', $id)->update($dataa);
            // 提交事务
            Db::commit();
            return json(['code' => 1, 'msg' => '请求成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage() . '，异常行：' . $e->getLine() . '，异常路径：' . $e->getfile()]);
        }
    }


    /**
     * 初级审核退回
     *
     */
    //退回修改
    public function tuihui()
    {
        $param             = $this->request->param();
        $User              = $this->getUser(1);
        $post              = $param['data'];
        $id                = $param['id'];
        $data['did']       = $param['id'];
        $data['shen_time'] = time();
        $data['shen_peo']  = $User['admin']['username'];
        $data['user_id']   = $User['admin']['id'];
        // 启动事务
        Db::startTrans();
        try {
            if ($post['shenhe_yijian'] == '') {
                $message = '请填写审核意见';
                return json(['code' => -1, 'msg' => $message]);
            } else {
                $data['shen_yijian'] = $post['shenhe_yijian'];
                $res                 = Db::name('yijian')->insert($data);
                $dataa['status']     = 4;
                $ress                = Db::name('list_one')->where('id', $id)->update($dataa);
            }
            // 提交事务
            Db::commit();
            return json(['code' => 1, 'msg' => '操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage() . '，异常行：' . $e->getLine() . '，异常路径：' . $e->getfile()]);
        }
    }


    /**
     * 财务审核退回
     *
     */
    //退回修改
    public function caiwu_tuihui()
    {
        $param = $this->request->param();

        $User              = $this->getUser(1);
        $post              = $param['data'];
        $id                = $param['id'];
        $data['did']       = $param['id'];
        $data['shen_time'] = time();
        $data['shen_peo']  = $User['admin']['username'];
        $data['user_id']   = $User['admin']['id'];
        // 启动事务
        Db::startTrans();
        try {
            if ($post['shenhe_yijian'] == '') {
                $message = '请填写审核意见';
                return json(['code' => -1, 'msg' => $message]);
            } else {
                $data['shen_yijian'] = $post['shenhe_yijian'];
                $res                 = Db::name('yijian')->insert($data);
                $dataa['status']     = 1;
                $ress                = Db::name('list_one')->where('id', $id)->update($dataa);
            }
            // 提交事务
            Db::commit();
            return json(['code' => 1, 'msg' => '操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage() . '，异常行：' . $e->getLine() . '，异常路径：' . $e->getfile()]);
        }
    }

    public function zuihou()
    {
        $param             = $this->request->param();
        $User              = $this->getUser(1);
        $id                = $param['id'];
        $data['did']       = $param['id'];
        $data['shen_time'] = time();
        $data['shen_peo']  = $User['admin']['username'];
        $data['user_id']   = $User['admin']['id'];

        $dataa['status']         = 5;
        $dataa['yw_shenhe_time'] = time();
        $dataa['yw_finance_peo'] = $User['admin']['username'];
        $dataa['pay_fangshi'] = $User['admin']['username'];
        $dataa['pay_type'] = 2;
        $area_code               = '';
        $lo                      = Db::name('list_one')->where('id', $id)
            ->field('id,ids,yw_come,yw_class,yw_peo,yw_peo_phone,yw_belonger,yw_handle,yw_join_time,yw_enter_peo,yw_hetongpeo,yw_hetong_time,yw_finance_peo,yw_shenhe_time,tong_storage,tong_number,tong_document,give_company,give_starttime,give_endtime,car_num,car_is_new,car_num_kind,haocolor,car_province,car_check,car_check_res,car_owner,car_card_kind,car_card,car_relation,car_address,car_vin,car_engine,car_factory_num,car_register,car_age,car_issue_time,car_is_transfer,car_buy,car_reall,car_use_nature,car_leixing,car_kind,car_carry_peo,car_carry_quality,car_curb_weight,car_displacement,car_power,car_lastyear,car_rank,plan_strattime,plan_endtime,main_name,main_money,main_standard,main_discount,main_submit,main_franchise,main_disan,main_santong,main_biao,main_zhe,main_yingjiao,main_ji,dao_name,dao_tong,dao_biao,dao_zhe,dao_ying,dao_ji,siji,siji_tong,siji_biao,siji_zhe,siji_ying,siji_ji,chengke,ck_money,chengke_zuo,ck_tong,ck_biao,ck_zhe,ck_ying,ck_ji,buji,bu_tong,bu_biao,bu_zhe,bu_ying,bu_buji,ziran,zi_tong,zi_biao,zi_zhe,zi_ying,zi_buji,fdjsunshi,fdj_tong,fdj_biao,fdj_zhe,fdj_ying,fdj_buji,zdxiuli,zd_tong,zd_biao,zd_zhe,zd_ying,zd_buji,huowu,hw_tong,hw_biao,hw_zhe,hw_ying,hw_buji,boli,bl_tong,bl_biao,bl_zhe,bl_ying,bl_buji,main_sum,main_sumdiscount,main_free,main_netrate,main_netcost,ginseng_name,ginseng_kind,ginseng_card,ginseng_phone,ginseng_email,ginseng_address,learned_name,learned_kind,learned_num,learned_phone,learned_email,learned_address,resolvent,special,invoice_kind,invoice_name,invoice_num,invoice_bank,bank_address,invoice_phone,beizhu,zhanghao,file,file_name,shuoming,shenhe_time,shenhe_peo,shenhe_yijian,status,statuss,pigai_type,pigai_numz,pigai_nump,pigai_piwen,pigai_ctime,pigai_pddzh,main_tongchoufee_heji,main_xiaoguan_bili,main_xiaoguan,main_yunying_bili,main_yunying,main_tongchoujingfee_heji,dong_fee,ncd,give_number,kehu_sumdiscount,chexing,chexing_ci,feiyong,jiafei,jiaofei,jiaofei_price,jiaofei_time,jiaofei_type,trade_no,out_trade_no,type,lururen,lururen_id,feiyong_type,feiyong_qita,jiafei_type,shui_lv1,shui_lv,ziran_c,fd_c,zd_c,huowu_c,boli_c,urls1,urls2,tcxm,status_type,qianzi_status,pdf,chezhu,tk_type,bjt,gs_type,zq_id')
            ->find();
        if ($lo != null && $lo['yw_class'] != null) {
            $zz = Db::name('zuzhi')->where(['id' => $lo['yw_class']])->find();
            if ($zz != null) {
                $area_code = $zz['area_code'];
            }
        }
        if (strlen($area_code) != 4) {
            $area_code = '0311';
        }
        if ($param['shenhe_yijian'] == '') {
            $data['shen_yijian'] = '财务审核(服务单)通过';
        } else {
            $data['shen_yijian'] = $param['shenhe_yijian'];
        }
        // 启动事务
        Db::startTrans();
        try {
            $res                  = Db::name('yijian')->insert($data);
            $table                = 'list_one';
            $field                = 'tong_number';
            $prev                 = 'HZZX';
            $tong_number          = $this->gen_tong_number($table, $field, $prev, $area_code);
            $dataa['tong_number'] = $tong_number;
            $ress                 = Db::name('list_one')->where('id', $id)->update($dataa);
            // 提交事务
            Db::commit();
            return json(['code' => 1, 'msg' => '请求成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage() . '，异常行：' . $e->getLine() . '，异常路径：' . $e->getfile()]);
        }

    }
    private function gen_tong_number($table, $field, $prev, $area_code) {
        $tong_number = null;
        $year = date('Ymd', time());
        $next_key = 'next_'.$table.'_'.$year;
        $tmp_number = db('system_config')->field('data')->where('id', $next_key)->find();
        if ($tmp_number == null) {
            $tmp_number = 1;
            db('system_config')->insert(['id'=>$next_key, 'data'=>$tmp_number]);
        } else {
            $tmp_number = intval($tmp_number['data']);
        }
        db('system_config')->where('id',$next_key)->update(['id'=>$next_key, 'data'=>$tmp_number+1]);
        $billWaterNo = str_pad($tmp_number, 7, '0', STR_PAD_LEFT);
        $tong_number =  $prev . $area_code . $year . $billWaterNo;
        return $tong_number;
    }
}